package com.plian.system.service.pf.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plian.Tools.CollectionUtil;
import com.plian.system.entity.pf.AppraisalIndex;
import com.plian.system.mapper.pf.AppraisalIndexMapper;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.mybatis.support.Query;
import com.plian.system.node.ForestNodeMerger;
import com.plian.system.service.pf.IAppraisalIndexService;
import com.plian.system.vo.pf.AppraisalIndexVO;
import com.plian.system.wrapper.pf.AppraisalIndexWrapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Take-off
 * @Description: TODO 绩效考评指标 服务实现类
 * @Date: 2022/12/14 9:29 AM
 * @Version: 1.0
 **/
@Service
@AllArgsConstructor
public class AppraisalIndexServiceImpl extends BaseServiceImpl<AppraisalIndexMapper, AppraisalIndex> implements IAppraisalIndexService {

    private AppraisalIndexWrapper appraisalIndexWrapper;

    /**
     * @Author: Take-off
     * @Description: //TODO 根据code获取列表
     * @Date: 5:32 PM 2022/12/19
     * @Param: [code]
     * @return: java.util.List<com.plian.system.entity.pf.AppraisalIndex>
     **/
    @Override
    public List<AppraisalIndex> listByTableCode(String code){
        return list(new LambdaQueryWrapper<AppraisalIndex>()
                .eq(AppraisalIndex::getTableCode, code));
    }

    @Override
    public List<AppraisalIndex> listByTableCodeAndYear(String code, String year) {
        return list(new LambdaQueryWrapper<AppraisalIndex>()
                .like(AppraisalIndex::getYear, year)
                .like(AppraisalIndex::getTableCode, code)
                .orderByAsc(AppraisalIndex::getSort));
    }

    @Override
    public ListResult<AppraisalIndexVO> listForTree(Query query) {
        List<AppraisalIndex> allList = list(new LambdaQueryWrapper<AppraisalIndex>().orderByAsc(AppraisalIndex::getTableCode,AppraisalIndex::getSort));
        List<AppraisalIndexVO> merge = ForestNodeMerger.merge(appraisalIndexWrapper.entityToVO(allList));

        ListResult<AppraisalIndexVO> listResult = new ListResult(CommonCode.SUCCESS);
        listResult.setList(merge);
        return listResult;
    }

    @Override
    public ListResult<AppraisalIndexVO> treeByTableCode(String tableCode) {
        ListResult<AppraisalIndexVO> listResult = new ListResult(CommonCode.SUCCESS);
        List<AppraisalIndex> allList = list(new LambdaQueryWrapper<AppraisalIndex>().eq(AppraisalIndex::getTableCode,tableCode).orderByAsc(AppraisalIndex::getTableCode,AppraisalIndex::getSort));
        if (CollectionUtil.isNotEmpty(allList)){
            List<AppraisalIndexVO> merge = ForestNodeMerger.merge(appraisalIndexWrapper.entityToVO(allList));
            listResult.setList(merge);
        }
        return listResult;
    }

}
